package com.hugoo.imp.buzz;

import com.hugoo.core.db.ImpProc;
import java.io.File;
import java.sql.PreparedStatement;
import java.util.Map;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

/**
 *
 * @author Lovelock.Chan
 */
public class XlsImpBuzz extends ImpInfo {

    public XlsImpBuzz(String _sid, String impFilePath, String _scod, Map<String, String> parax) {
        super(_sid, impFilePath, _scod, parax);
    }

    @Override
    public ImpProc impProc() {
        return new ImpProc() {
            public String procSql() {
                return kf;
            }

            public void preProc(PreparedStatement psmt) {
                try {
                    Workbook wb = Workbook.getWorkbook(new File(impFilePath));
                    Sheet sheet = wb.getSheet(0);
                    int columns = sheet.getColumns();		// 获取Excel表的列数
                    int rows = sheet.getRows();
                    int l = _cols.length;
                    if (columns != l) {
                        wb.close();
                        log.info("导入失败,数据列长度不符！");
                    } else {
                        Cell[] cl;
                        for (int i = 1; i < rows; i++) {//
                            cl = sheet.getRow(i);
                            for (int j = 0; j < cl.length; j++) {
                                psmt.setString((j + 1), cl[j].getContents());
                            }
                            psmt.addBatch();
                        }
                        psmt.executeBatch();
                    }
                    log.info("本次导入数据，导入【" + (rows - 1) + "】条记录");
                } catch (Exception ex) {
                    log.warn("Excel导入出现异常！", ex);
                }
            }
        };
    }
}
